package com.gzdh.dishrecognition.ui.adapter;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.fastjson.JSON;
import com.dhkj.web.bean.MacMainpFoodMenu;
import com.gzdh.dishrecognition.R;
import com.gzdh.dishrecognition.ui.dialog.FoodsEditFragment;
import com.gzdh.dishrecognition.ui.dialog.identify.LoginFragment;

import java.util.ArrayList;
import java.util.List;

public class FoodsCardSetAdapter extends RecyclerView.Adapter<FoodsCardSetAdapter.ViewHolder> {

    private Context mContext;

    private FragmentManager manager;

    private List<MacMainpFoodMenu> list;

    private OnClick onClick;

    public void setOnClick(OnClick onClick) {
        this.onClick = onClick;
    }

    public interface OnClick {
        void makeClick(MacMainpFoodMenu macMainpFoodMenu);
    }

    public FoodsCardSetAdapter(Context context, FragmentManager manager) {
        mContext = context;
        this.manager = manager;
        this.list = new ArrayList<>();
    }


    public void setList(List<MacMainpFoodMenu> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_foods_card, parent, false);
        return new ViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.idTv.setText(String.valueOf(position + 1));
        holder.nameTv.setText(list.get(position).getName());
        holder.priceTv.setText(list.get(position).getPrice());
        holder.cardNumTv.setText(list.get(position).getCardId());
        holder.makeBut.setOnClickListener(view -> {
            if (onClick != null) {
                onClick.makeClick(list.get(position));
            }
        });
        holder.updBut.setOnClickListener(view -> {
            FoodsEditFragment foodsEditFragment = new FoodsEditFragment();
            Bundle bundle = new Bundle();
            bundle.putString("foodMenu", JSON.toJSONString(list.get(position)));
            foodsEditFragment.setArguments(bundle);
            foodsEditFragment.show(manager, LoginFragment.class.getSimpleName());
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        LinearLayout mainLinear;

        TextView idTv;
        TextView nameTv;
        TextView priceTv;
        TextView cardNumTv;
        TextView makeBut;
        TextView updBut;
        TextView delBut;

        ViewHolder(View item) {
            super(item);
            mainLinear = item.findViewById(R.id.mainLinear);

            idTv = item.findViewById(R.id.idTv);
            nameTv = item.findViewById(R.id.nameTv);
            priceTv = item.findViewById(R.id.priceTv);
            cardNumTv = item.findViewById(R.id.cardNumTv);
            makeBut = item.findViewById(R.id.makeBut);
            updBut = item.findViewById(R.id.updBut);
            delBut = item.findViewById(R.id.delBut);
        }

    }


}
